﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.IO;

namespace MotelServices
{
    public class DiaChiBUS
    {
        public static string _connString = string.Empty;

        #region Static Contructor
        static DiaChiBUS()
        {
            _connString = ConfigurationManager.ConnectionStrings["MotelConnectionString"].ConnectionString.ToString();
        }
        #endregion

        #region update address information function
        public static bool updateAddress(DiaChiDTO requiredAddress)
        {
            bool result = true;
            DataMotelDataContext dbContext = new DataMotelDataContext(_connString);
            try
            {
                if (requiredAddress == null)
                {
                    throw new Exception("Input invalid parameter");
                }
                DiaChiDTO localAddress = requiredAddress;
                int rs = dbContext.spUpdateAddress(localAddress.MaDiaChi, localAddress.ViDo,
                     localAddress.KinhDo);
                if (rs == -1)
                {
                    throw new Exception("Update not successfully");
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                dbContext.Dispose();
            }
            return result;
        }
        #endregion

        #region delete address information function
        public static bool deleteAddress(int idAddress)
        {
            bool result = true;
            DataMotelDataContext dbContext = new DataMotelDataContext(_connString);
            try
            {
                if (idAddress == 0)
                {
                    throw new Exception("Input invalid parameter");
                }
                int localIdAddress = idAddress;
                int rs = dbContext.spDeleteAddress(localIdAddress);
                if (rs == -1)
                {
                    throw new Exception("Delete not successfully");
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                dbContext.Dispose();
            }
            return result;
        }
        #endregion

        #region Function to get all address
        public static List<DiaChiDTO> LayDiaChi()
        {
            List<DiaChiDTO> ldc = new List<DiaChiDTO>();
            DataMotelDataContext dtdc = new DataMotelDataContext(_connString);
            try
            {
                var DiaChiObj = (from dc in dtdc.DiaChis
                                 where dc.TinhTrang.Equals(1)
                                 select dc);
                foreach (var item in DiaChiObj)
                {
                    DiaChiDTO dc = new DiaChiDTO();
                    dc.MaDiaChi = Convert.ToInt32(item.MaDiaChi);
                    dc.SoNha = item.SoNha;
                    dc.TenDuong = item.TenDuong;
                    dc.MaPhuong = Convert.ToInt32(item.MaPhuong);
                    dc.MaQuan = Convert.ToInt32(item.MaQuan);
                    dc.KinhDo = (float)item.KinhDo;
                    dc.ViDo = (float)item.ViDo;
                    ldc.Add(dc);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                dtdc.Dispose();
            }
            return ldc;
        }
        #endregion

        #region Function to get all address
        public List<DiaChiDTO> LayTatCaDiaChi()
        {
            List<DiaChiDTO> ldc = new List<DiaChiDTO>();
            DataMotelDataContext dtdc = new DataMotelDataContext(_connString);
            try
            {
                var DiaChiObj = (from dc in dtdc.DiaChis
                                 select dc);
                foreach (var item in DiaChiObj)
                {
                    DiaChiDTO dc = new DiaChiDTO();
                    dc.MaDiaChi = Convert.ToInt32(item.MaDiaChi);
                    dc.SoNha = item.SoNha;
                    dc.TenDuong = item.TenDuong;
                    dc.MaPhuong = Convert.ToInt32(item.MaPhuong);
                    dc.MaQuan = Convert.ToInt32(item.MaQuan);
                    dc.KinhDo = (float)item.KinhDo;
                    dc.ViDo = (float)item.ViDo;
                    ldc.Add(dc);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                dtdc.Dispose();
            }
            return ldc;
        }
        #endregion

        #region Function to insert new address updated by hnmtuan
        public static bool insertAddress(DiaChiDTO paramAddress)
        {
            bool result = true;
            DataMotelDataContext dbContext = new DataMotelDataContext(_connString);
            try
            {
                if (paramAddress == null)
                {
                    throw new Exception("Input invalid parameter");
                }
                DiaChiDTO localAddress = paramAddress;
                // Prepare object to insert
                int rs = dbContext.spInsertAddress(localAddress.SoNha, localAddress.TenDuong,
                    localAddress.MaQuan, localAddress.MaPhuong,
                    localAddress.ViDo, localAddress.KinhDo,
                    localAddress.TinhTrang);
                if (rs == -1)
                {
                    throw new Exception("Insert not successfully");
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                dbContext.Dispose();
            }
            return result;
        }
        #endregion

        #region Function to insert new address
        public int ThemDiaChi_Linq(string sonha, string tenduong, int maphuong, int maquan, float kinhdo, float vido)
        {
            int kq = 1;
            DataMotelDataContext dtdc = new DataMotelDataContext(_connString);
            try
            {
                DiaChi dc = new DiaChi();
                dc.SoNha = sonha;
                dc.TenDuong = tenduong;
                dc.MaPhuong = maphuong;
                dc.MaQuan = maquan;
                dc.KinhDo = kinhdo;
                dc.ViDo = vido;
                dtdc.DiaChis.InsertOnSubmit(dc);
                dtdc.SubmitChanges();
            }
            catch (Exception)
            {
                kq = 0;
            }
            finally
            {
                dtdc.Dispose();
            }
            return kq;
        }
        #endregion

        #region Function to update address
        public int CapNhatDiaChi_Linq(DiaChiDTO dci)
        {
            int kq = 1;
            DataMotelDataContext dtdc = new DataMotelDataContext(_connString);
            try
            {
                DiaChi dc = dtdc.DiaChis.Single(p => p.MaDiaChi == dci.MaDiaChi);
                dc.SoNha = dci.SoNha;
                dc.TenDuong = dci.TenDuong;
                dc.MaPhuong = dci.MaPhuong;
                dc.MaQuan = dci.MaQuan;
                dc.KinhDo = dci.KinhDo;
                dc.ViDo = dci.ViDo;
                dtdc.SubmitChanges();
            }
            catch (Exception)
            {
                kq = 0;
            }
            finally
            {
                dtdc.Dispose();
            }
            return kq;
        }
        #endregion

        #region Function to delete address
        public int XoaDiaChi_LinQ(DiaChiDTO dci, int loai)
        {
            int kq = 1;
            DataMotelDataContext dtdc = new DataMotelDataContext(_connString);
            try
            {
                DiaChi dc = dtdc.DiaChis.Single(p => p.MaDiaChi == dci.MaDiaChi);
                if (loai == 0)
                {
                    dc.TinhTrang = 1;
                }
                else
                {
                    dc.TinhTrang = 0;
                }
                dtdc.SubmitChanges();
            }
            catch (Exception)
            {
                kq = 0;
            }
            finally
            {
                dtdc.Dispose();
            }
            return kq;
        }
        #endregion

        #region Function to result search
        public static List<DiaChiDTO> searchaddress(int quan, int phuong)
        {
            List<DiaChiDTO> ldc = new List<DiaChiDTO>();
            DataMotelDataContext dtdc = new DataMotelDataContext(_connString);
            try
            {
                var DiaChiObj = (from dc in dtdc.DiaChis
                                 where dc.MaQuan.Equals(quan) && dc.MaPhuong.Equals(phuong)
                                 select dc);
                foreach (var item in DiaChiObj)
                {
                    DiaChiDTO dc = new DiaChiDTO();
                    dc.MaDiaChi = Convert.ToInt32(item.MaDiaChi);
                    dc.SoNha = item.SoNha;
                    dc.TenDuong = item.TenDuong;
                    dc.MaPhuong = Convert.ToInt32(item.MaPhuong);
                    dc.MaQuan = Convert.ToInt32(item.MaQuan);
                    dc.KinhDo = (float)item.KinhDo;
                    dc.ViDo = (float)item.ViDo;
                    ldc.Add(dc);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                dtdc.Dispose();
            }
            return ldc;
        }
        #endregion
    }
}